#include "gd32f30x.h"
#include "delay.h"
#include "led_drv.h"
#include "key_drv.h"
#include "systick.h"
#include "usart_drv.h"
#include "usart_data_packet_analysis_app.h"
#include "task_drv.h"
#include "timer0_drv.h"
#include "timer1_drv.h"
#include "ir_drv.h"
#include <stdbool.h>
#include "rtc_drv.h"
#include "task_app.h"
#include "wdgt_drv.h"
#include "adc_drv.h"
#include "modbus_app.h"
#include "eeprom_drv.h"
#include "key_drv.h"
#include "store_app.h"
#include "norflash_drv.h"
#include "self_flash_drv.h"
#include "fatfs_app.h"
#include "lvgl_app.h"
#ifdef SYS_SUPPORT_OS
#include "FreeRTOS.h"
#include "task.h"
#endif
#include "can_drv.h"
#include "wifi_app.h"
/******************基础配置**************************/
static void DrvInit(void) {
    LedDrvInit();
    DelayInit();
    UsartDrvInit();
    regUsartInteruptCallFunc(DataPacketGetData);
//    FWDGT_config();
    SensorDrvInit();
    ModebusAppInit();
    EepromDrvInit();
    KeyDrvInit();
    NorFlashDrvInit();
    RtcTime_t rtcTime = { .year = 2025,.month = 5,.day = 14,.hour = 9,.minute = 28,.second = 20 };
    RtcDrvInit(&rtcTime);
    SetRtcTime(&rtcTime);
    CANDrvInit();
    WIFIDrvInit();
		DelayNms(1000);
}

static void AppInit(void) {
    DataPacketProtocolAnalysisInit();
    InitSysParam();
    FatfsInit();
    //    LvglAppInit();
}


/*******************区分裸机与RTOS*************************/
#ifdef SYS_SUPPORT_OS
#include "FreeRTOS.h"
#include "task.h"

// 任务句柄
TaskHandle_t            start_handler;
TaskHandle_t            task1_handler;
void task1(void* pvParameters) {
    while (1) {
        vTaskDelay(1000);
        gpio_bit_set(GPIOD, GPIO_PIN_7);
        vTaskDelay(1000);
        gpio_bit_reset(GPIOD, GPIO_PIN_7);
    }
}

// 开始任务
void start_task(void* pvParameters) {
    taskENTER_CRITICAL();
    xTaskCreate(task1, "task1", 64, NULL, 2, &task1_handler);
    vTaskDelete(start_handler);
    vTaskSuspend(start_handler);
    taskEXIT_CRITICAL();
}


int main(void) {
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
    DrvInit();
    printf("init\n");
    AppInit();
    xTaskCreate(start_task, "start_task", 128, NULL, 1, &start_handler);
    vTaskStartScheduler();
    xSemaphoreGive();
    while (1) {}
}


#else
int main() {
    nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
    SystickInit();
    DrvInit();
    printf("init\n");
    // AppInit();
    TaskDrvInit();
//    TaskDrvReg("FwdgtTask", 40, FWDGT_feed);
//    TaskDrvReg("SensorDrvProc", 100, SensorDrvProc);
//	  TaskDrvReg("SensorDrvProc", 1000, SensorResultTask);
    //    TaskDrvReg("LvglCheckFlush", 1, LvglCheckFlush);
    //    TaskDrvReg("DataPacketProtocolAnalysis", 20, DataPacketProtocolAnalysis);
    // TaskDrvReg("CANDrvTest", 1000, CANDrvTest);
	  TaskDrvReg("WifiNetWorkTask", 100, WifiNetWorkTask);
    while (1) {
        TaskDrvStartSchedule();
    }
}
#endif 


